<template>
  <div class="course-content-list">
    <!-- 下拉刷新 -->
    <van-pull-refresh v-model="isRefreshing" @refresh="onRefresh">
      <!-- 课程列表 -->
      <van-list
        v-model="loading"
        :finished="finished"
        :error.sync="error"
        error-text="请求失败，点击重新加载"
        finished-text="没有更多了"
        @load="onload"
      >
        <van-cell
          v-for="item in list"
          :key="item.id"
          @click="
            $router.push({ name: 'course-info', params: { courseId: item.id } })
          "
        >
          <div>
            <!-- 所有商品与已购商品接口响应的图片地址属性名不同，需要检测 -->
            <img :src="item.courseImgUrl || item.image" />
          </div>
          <div class="course-info">
            <!-- 课程名称检测 -->
            <h3 v-text="item.courseName || item.name"></h3>
            <p class="course-preview" v-html="item.previewFirstField"></p>
            <!-- 检测是否有价格，如果没有，说明为已购商品，不展示价格区域 -->
            <p v-if="item.price" class="price-container">
              <span class="course-discounts">￥{{ item.discounts }}</span>
              <del class="course-price">￥{{ item.price }}</del>
            </p>
          </div>
        </van-cell>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import { PullRefresh, List, Cell, Toast } from 'vant'
export default {
  name: 'CourseContentList',
  components: {
    VanPullRefresh: PullRefresh,
    VanList: List,
    VanCell: Cell
  },
  props: {
    callback: {
      type: Function,
      require: true
    }
  },
  data () {
    return {
      // 存储数据
      list: [],
      //   是否加载中
      loading: false,
      //   是否加载完毕
      finished: false,
      //   数据页数
      currentPage: 1,
      //   加载错误
      error: false,
      // 下拉刷新状态
      isRefreshing: false
    }
  },
  created () {
    this.onload()
  },
  methods: {
    // 下拉刷新
    async onRefresh () {
      // 下拉刷新页码初始化
      this.currentPage = 1
      const { data } = await this.callback({
        currentPage: this.currentPage,
        pageSize: 10,
        status: 1
      })

      // 避免空数据
      if (data.data && data.data.records && data.data.records.length) {
        this.list = data.data.records
      } else if (data.content && data.content.length) {
        this.list = data.content
      }
      Toast('刷新成功')
      // 下拉刷新状态关闭
      this.isRefreshing = false
    },
    // 加载课程
    async onload () {
      const { data } = await this.callback({
        currentPage: this.currentPage,
        pageSize: 10,
        status: 1
      })
      //   console.log(data)
      // 避免空数据
      if (data.data && data.data.records && data.data.records.length) {
        this.list.push(...data.data.records)
      } else if (data.content && data.content.length) {
        this.list.push(...data.content)
      }

      //   数据加载完毕
      this.loading = false
      // 加载下一页数据
      this.currentPage++
      if (data.data && data.data.records && data.data.records < 10) {
        this.finished = true
      } else if (data && data.content) {
        this.finished = true
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.course-content-list {
  width: 100%;
  position: fixed;
  overflow-y: auto;
}
.van-pull-refresh {
  // height: 100%;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  overflow-y: auto;
}
.van-list {
  height: 100%;
}
// 课程条目设置flex，内部元素左右显示
.van-cell__value {
  height: 100px;
  padding: 10px 0;
  display: flex;
}

// 左侧图设置固定尺寸
.van-cell__value img {
  width: 75px;
  height: 100%;
  border-radius: 6px;
}

// 右侧内容区域 flex: 1 分配右侧剩余空间
.course-info {
  flex: 1;
  height: 100%;
  padding: 0 10px;
  display: flex;
  flex-direction: column;
  h3 {
    margin: 0;
    line-height: 24px;
  }
  .course-preview {
    flex-grow: 1;
    font-size: 14px;
    line-height: 16px;
    margin: 0;
  }
  .price-container {
    margin: 0;
  }
  .course-discounts {
    color: #ff7452;
    font-size: 14px;
    margin: 0;
    margin-right: 5px;
  }
}
</style>
